package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.WorkPlanAssessment;
import com.amsp.oa.oademo.entity.WorkPlanCategory;
import com.amsp.oa.oademo.service.WorkPlanAssessmentService;
import com.amsp.oa.oademo.service.WorkPlanCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/work-plan-assessment")
@Api(tags = "工作计划考核接口")
@CrossOrigin
public class WorkPlanAssessmentController {

    @Autowired
    private WorkPlanAssessmentService workPlanAssessmentService;

    @Autowired
    private WorkPlanCategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("获取工作计划考核列表")
    public ResponseEntity<IPage<WorkPlanAssessment>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String department,
            @RequestParam(required = false) String position,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        return ResponseEntity
                .ok(workPlanAssessmentService.getPage(current, size, name, department, position, year, month));
    }

    @GetMapping("/view/{id}")
    @ApiOperation("获取工作计划考核详情")
    public ResponseEntity<WorkPlanAssessment> getById(@PathVariable Long id) {
        WorkPlanAssessment assessment = workPlanAssessmentService.getDetailById(id);
        return ResponseEntity.ok(assessment);
    }

    @PostMapping("/save")
    @ApiOperation("保存工作计划考核")
    public ResponseEntity<WorkPlanAssessment> save(@RequestBody WorkPlanAssessment workPlanAssessment) {
        if (workPlanAssessment.getId() != null) {
            throw new RuntimeException("新增时不应该包含ID");
        }
        return ResponseEntity.ok(workPlanAssessmentService.saveWithDetails(workPlanAssessment));
    }

    @PutMapping("/update")
    @ApiOperation("更新工作计划考核")
    public ResponseEntity<WorkPlanAssessment> update(@RequestBody WorkPlanAssessment workPlanAssessment) {
        if (workPlanAssessment.getId() == null) {
            throw new RuntimeException("更新时ID不能为空");
        }
        return ResponseEntity.ok(workPlanAssessmentService.saveWithDetails(workPlanAssessment));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除工作计划考核")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        workPlanAssessmentService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/categories")
    @ApiOperation("获取工作计划考核类别列表")
    public List<WorkPlanCategory> listCategories() {
        return categoryService.list();
    }
}